//跟网络相关的代码
#include <stdio.h>
#include <sys/types.h>        
#include <sys/socket.h>
#include "main.h"
#include <pthread.h>
#include "socket.h"
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
#include <pthread.h>
#include <netinet/in.h>
#include <signal.h>

#include <json-c/json.h>

#include <sys/select.h>
#include "link.h"
#include "player.h"

fd_set readFds;
int g_maxFd = -1;

extern int g_sockfd;
extern int g_ledfd0_brightness;
extern int g_ledfd1_brightness;

extern Node* head;
extern shm *shmaddr;

extern int g_start_flag;
extern int g_suspend_flag;
extern long new_vol_left;

//1秒执行一次，向服务器发送alive字符串
void send_server(int signum)
{
        struct json_object* json=json_object_new_object();
        json_object_object_add(json,"cmd",json_object_new_string("info"));
        json_object_object_add(json,"status",json_object_new_string("alive"));
        json_object_object_add(json,"deviceid",json_object_new_string("001"));

        const char* buf=json_object_to_json_string(json);
        int ret=send(g_sockfd,buf,strlen(buf),0);
        if(-1==ret){
                perror("send");
        }
        alarm(TIMEOUT); 
}

//连接服务器，连接5次
void* connect_cb(){
        int count=5,ret;

        struct sockaddr_in server_addr;
        memset(&server_addr,0,sizeof(server_addr));
        server_addr.sin_family=AF_INET;
        server_addr.sin_port=htons(SERVER_PORT);
        server_addr.sin_addr.s_addr=inet_addr(SERVER_IP);

        while(count--){
                ret=connect(g_sockfd,(struct sockaddr *)&server_addr,sizeof(server_addr));
                if(ret==-1){
                        sleep(5);       //连接不成功，则间隔5秒再次连接
                        continue;
                }
                        
                //连接成功则DS0，DS1都亮
                write(g_ledfd0_brightness, "1", 1);
                write(g_ledfd1_brightness, "1", 1);

                //5秒后向线程一个SIGNALRM信号，即5秒后开始发送保活信息
                alarm(5);
                signal(SIGALRM,send_server);

                //添加到文件描述符集合中
                FD_SET(g_sockfd,&readFds);
                if(g_sockfd > g_maxFd)
                {
                        g_maxFd = g_sockfd;
                }
                printf("g_sockfd = %d\n",g_sockfd);

                break;
                
        }
        return NULL;
        
}


int InitSocket()
{
        FD_ZERO(&readFds);

        //创建socket
        g_sockfd=socket(AF_INET,SOCK_STREAM,0);
        if(g_sockfd==-1){
                return FAILURE;
        }

        //启动线程，向服务器发起连接。在这个线程里面初始化了g_sockfd
        pthread_t tid;
        int ret=pthread_create(&tid,NULL,connect_cb,NULL);
        if(ret!=0){
                return FAILURE;
        }
        return SUCCESS;
}

void socket_get_status()
{
        //回复音响状态
        struct json_object* json=json_object_new_object();
        json_object_object_add(json,"cmd",json_object_new_string("reply_status"));

        if(g_start_flag==1 && g_suspend_flag==0){
                json_object_object_add(json,"status",json_object_new_string("start"));
        }
        
        if(g_start_flag==1 && g_suspend_flag==1){
                json_object_object_add(json,"status",json_object_new_string("suspend"));
        }

        if(g_start_flag==0){
                json_object_object_add(json,"status",json_object_new_string("stop"));
        }
        json_object_object_add(json,"music",json_object_new_string(shmaddr->cur_name));

        json_object_object_add(json,"voice",json_object_new_int64(new_vol_left));
                
        const char* buf=json_object_to_json_string(json);
        int ret=send(g_sockfd,buf,strlen(buf),0);
        if(-1==ret){
                perror("send");
        }  
}

void socket_get_music()
{
        //回复所有音乐
        struct json_object* json=json_object_new_object();
        json_object_object_add(json,"cmd",json_object_new_string("reply_music"));

        struct json_object* array=json_object_new_array();
        Node* p=head->next;
        while(p!=head)
        {
                json_object_array_add(array,json_object_new_string(p->music_name));
                p=p->next;
        }
        json_object_object_add(json,"music",array);
        const char* buf=json_object_to_json_string(json);
        int ret=send(g_sockfd,buf,strlen(buf),0);
        if(-1==ret){
                perror("send");
        }  
}